package com.mall.mymall.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.mymall.dao.dto.*;
import com.mall.mymall.dao.entity.ProductComments;
import com.mall.mymall.dao.entity.Users;
import com.mall.mymall.mapper.ProductCommentsMapper;
import com.mall.mymall.service.ProductCommentsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 十里坡
 * @since 2025-04-01
 */
@Service
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements ProductCommentsService {
    @Override
    public QueryDto<CommentsDto> pageMyComments(PageRequestDto pageRequestDto, Users users) {
        // 创建分页对象
        Page<CommentsDto> page = new Page<>(pageRequestDto.getPage(), pageRequestDto.getSize());
        Integer userId = users.getUserId();
        // 执行自定义分页查询
        IPage<CommentsDto> resultPage = this.baseMapper.selectMyComments(page, userId);

        // 构造返回结果
        QueryDto<CommentsDto> result = new QueryDto<>();
        result.setTotal((int) resultPage.getTotal());
        result.setRecords(resultPage.getRecords());
        return result;
    }

    @Override
    public QueryDto<CommentsDto> pagePendingComments(PageRequestDto pageRequestDto, Users users) {
        // 创建分页对象
        Page<CommentsDto> page = new Page<>(pageRequestDto.getPage(), pageRequestDto.getSize());
        Integer userId = users.getUserId();
        // 执行自定义分页查询
        IPage<CommentsDto> resultPage = this.baseMapper.selectPendingComments(page, userId);

        // 构造返回结果
        QueryDto<CommentsDto> result = new QueryDto<>();
        result.setTotal((int) resultPage.getTotal());
        result.setRecords(resultPage.getRecords());
        return result;
    }

    @Transactional
    @Override
    public boolean removeComments(Integer commentId) {
        this.baseMapper.removeComments(commentId);
        this.baseMapper.changeStatus(commentId);
        return true;
    }

    @Transactional
    @Override
    public boolean saveComments(SubmitCommentsDto submitCommentsDto) {
        String commentContent = submitCommentsDto.getCommentContent();
        Integer productId = submitCommentsDto.getProductId();
        Integer rating = submitCommentsDto.getRating();
        Integer userId = submitCommentsDto.getUsers().getUserId();
        this.baseMapper.saveCommentsContent(commentContent, productId, rating, userId);
        this.baseMapper.changeStatusByProductIdAndUserId(productId,userId);
        return true;
    }
}
